package com.edgclub.edg.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzBindPhomeActivity;
import com.edgclub.edg.activity.GzWebViewActivity;
import com.edgclub.edg.model.result.FindIndexData;
import com.edgclub.edg.uc.DeleteDialog;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 活动入口
 */
public class FindEntranceAdapter extends BaseAdapter {
    private Context mContext;

    private ArrayList<FindIndexData.Entrance> entrances;

    private GlideUtils glideUtils;

    private ViewHolder holder;
    private UserPrefUtils userPrefUtils;

    public FindEntranceAdapter(Context mContext) {
        this.mContext = mContext;
        glideUtils = new GlideUtils();
        userPrefUtils=new UserPrefUtils(mContext);
    }

    public ArrayList<FindIndexData.Entrance> getEntrances() {
        return entrances;
    }

    public void setEntrances(ArrayList<FindIndexData.Entrance> entrances) {
        this.entrances = entrances;
    }

    @Override
    public int getCount() {
        return entrances.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (null == convertView) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_find_entrance, null);
            holder = new ViewHolder();
            holder.imgv_entrance = (ImageView) convertView.findViewById(R.id.imgv_entrance);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_entrance_title);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_entrance_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final FindIndexData.Entrance entrance = entrances.get(position);
        holder.tv_title.setText(entrance.getTitle()+"");
        holder.tv_content.setText(entrance.getContent()+"");
        glideUtils.Glidde_loadCropCircleImage(mContext,entrance.getLogo(), holder.imgv_entrance);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=null;
                if(1==entrance.getType()) {
                    String gzno = userPrefUtils.getUser().getGzno();
                    String phone = userPrefUtils.getUser().getPhone();
                    if (gzno.equals(phone)) {
                        showDialog(entrance.getUrl());
                    } else {
//                        intent = new Intent(mContext, YouZanWebActivity.class);
//                        intent.putExtra("url", entrance.getUrl());
//                        mContext.startActivity(intent);
                    }
                }else {
                    intent = new Intent(mContext, GzWebViewActivity.class);
                    intent.putExtra("actbarTitle",entrance.getTitle());
                    intent.putExtra("url",entrance.getUrl());
                    mContext.startActivity(intent);
                }

                //统计点击事件
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("tid",entrance.getId());
                map.put("title",entrance.getTitle());
                MobclickAgent.onEvent(mContext, "EC_FIND_ENTRANCE", map);
            }
        });
        return convertView;
    }
    private void showDialog(final String shopUrl) {
        final DeleteDialog dialog = DeleteDialog.create(mContext, true);
        dialog.setTitle(StringUtils.getString(R.string.prompt));
        dialog.setContent("为了更加便捷的提供物流等信息服务，需要您绑定手机号码以后才可以访问周边商城哦。");
        dialog.show();
        dialog.getTvOk().setText(StringUtils.getString(R.string.ok));
        dialog.getTvOk().setTextColor(0xff0096ff);
        dialog.setCancelListener(new DeleteDialog.CancelClickListener() {

            @Override
            public void cancelClick() {
                dialog.dismiss();
            }
        });

        dialog.setSubmitListener(new DeleteDialog.SubmitClickListener() {

            @Override
            public void submitClick() {
                Intent intent = new Intent(mContext,
                        GzBindPhomeActivity.class);
                intent.putExtra("url", shopUrl);
                mContext.startActivity(intent);
                dialog.dismiss();
            }
        });
    }

    public class ViewHolder {

        public ImageView imgv_entrance;
        public TextView tv_title,tv_content;
    }


}
